package cn.wolfcode.service.impl;

import cn.wolfcode.domain.ActivetyEmployee;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.domain.Choice;
import cn.wolfcode.mapper.ActivetyEmployeeMapper;
import cn.wolfcode.mapper.ActivityItemMapper;
import cn.wolfcode.mapper.ChoiceMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IChoiceService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ChoiceServiceImpl implements IChoiceService {

    @Autowired
    private ChoiceMapper choiceMapper;
    @Autowired
    private ActivityItemMapper activityItemMapper;

    @Autowired
    private ActivetyEmployeeMapper activetyEmployeeMapper;

    @Override
    public void save(Choice choice) {
        choiceMapper.insert(choice);
    }

    @Override
    public void delete(Long id) {
        choiceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Choice choice) {
        choiceMapper.updateByPrimaryKey(choice);
    }

    @Override
    public Choice get(Long id) {
        return choiceMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Choice> listAll() {
        return choiceMapper.selectAll();
    }

    @Override
    public PageInfo<Choice> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Choice> choices = choiceMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Choice>(choices);
    }

    @Override
    public void saveByAcItemId(Long optionsRadios) {
        Choice choice = new Choice();
        choice.setActivityItemId(optionsRadios);
        choice.setCreateTime(new Date());
        choice.setEmployeeId(UserContext.getContextUser().getId());
        choiceMapper.insert(choice);
    }

    @Override
    public void updateByActId(Long actId) {

        activetyEmployeeMapper.update(actId,UserContext.getContextUser().getId());
    }
}
